#include "input.hpp"
#include "control.hpp"
#include <allegro.h>
#include <string>
#include "main.hpp"
#include <iostream>

void cInput::draw(BITMAP * &bufor)
{
    if (!show || deleted) return;
    if (size->x < 14 || size->y < 14) return;

    masked_blit(img, bufor, 0, 0, pos->x, pos->y, size->x, size->y);

    if (text_length(font, value.c_str()) > size->x - 6)
    {
        textprintf_ex(bufor, font, pos->x + 3, pos->y + size->y / 2 - 3, textColor, -1, "%s", value.substr(value.length() - (size->x * value.length()) / text_length(font, value.c_str()), (size->x * value.length()) / text_length(font, value.c_str())).c_str());
        //vline(bufor, pos->x + size->x - 10, pos->y + 5, pos->y + 13, textColor);
    }
    else
        textprintf_ex(bufor, font, pos->x + 3, pos->y + size->y / 2 - 3, textColor, -1, "%s", value.c_str());
}

cInput::cInput(int x, int y, int w, int h, char * v, int tCol, int bgCol) : cControl(x, y, w, h, v, tCol, bgCol)
{
    BITMAP * tmp = load_bmp("GFX\\gui\\input.bmp", default_palette);//wczytanie obrazka

    img = create_bitmap(w, h);
    clear_to_color(img, 0xFFF000);

    blit(tmp, img, 0, 0, 0, 0, 7, 7);//prawy gorny rog
    blit(tmp, img, 7, 0, w - 7, 0, 7, 7);//lewy gorny rog
    blit(tmp, img, 0, 7, 0, h - 7, 7, 7);//prawy dolny rog
    blit(tmp, img, 7, 7, w - 7, h - 7, 7, 7);//lewy dolny rog
    hline(img, 7, 0, w - 7, 0);//linia gorna
    hline(img, 7, h - 1, w - 7, 0);//linia dolna
    vline(img, 0, 7, h - 7, 0);//lewa linia
    vline(img, w - 1, 7, h - 7, 0);//prawa linia

    colorToColor(img, 0xFFF000, 0xFFFFFF);
}

cInput::~cInput()
{
    destroy_bitmap(img);
}
